#ifndef CS_ASYNC_CLIENT_H
#define CS_ASYNC_CLIENT_H

#include <deque>
#include <iostream>
#include <boost\function.hpp>
#include <boost\bind.hpp>
#include <boost\asio.hpp>
#include <boost\thread\thread.hpp>
#include <boost\shared_ptr.hpp>
#include "message.h"

namespace cs
{
	class async_client
	{
	public:
		typedef boost::function<void (message_handle)> callback_t;
		async_client(callback_t callback);
		/*async_client(boost::asio::io_service& io_service,
			boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
			*/
		void send(message& msg);
		void close();
		~async_client();
	private:
		typedef std::deque<cs::message> message_queue;
		typedef boost::shared_ptr<boost::thread> thread_t;
		typedef boost::asio::ip::tcp boost_asio_tcp;
		

		void handle_connect(const boost::system::error_code& error);
		void handle_read_header(const boost::system::error_code& error);
		void handle_read_body(const boost::system::error_code& error);
		void do_send(const cs::message& msg);
		void handle_send(const boost::system::error_code& error);
		void do_close();

	private:
		thread_t thread;
		boost::asio::io_service io_service_;
		boost::shared_ptr<boost_asio_tcp::socket> socket_;
		callback_t callback_;
		message_handle read_msg_;
		message_queue write_msgs_;
	};
} //end namespace cs
#endif // CS_ASYNC_CLIENT_H